#include "game.h"
//--------------------------------------------------------------------------------
//In game.h!

#include "global_variables.h"
Move_status move_Digdug(Digdug &movable, const Direction &direction);

private:

	void move_digdug_up(Movable &movable, Move_status &final_move_status);
	void move_digdug_down(Movable &movable, Move_status &final_move_status);
	void move_digdug_left(Movable &movable, Move_status &final_move_status);
	void move_digdug_right(Movable &movable, Move_status &final_move_status);
	



//------------------------------------------------------------------------------------

void Game::move_digdug_up( Movable &movable, Move_status &final_move_status)
{
	// return the x-location of the center of the object
	int start_movable_center_x = movable.get_x_location(); 
// return the y-location of the center of the object
	int start_movable_center_y = movable.get_y_location();
// get the coordinates and dimensions of the movable
	int start_top_of_movable = start_movable_center_y - movable.get_y_boundary();
	int start_bottom_of_movable = start_movable_center_y + movable.get_y_boundary();
	int start_LHS_of_movable = start_movable_center_x - movable.get_x_boundary();
	int start_RHS_of_movable = start_movable_center_x + movable.get_x_boundary();
	int height_of_movable = 2*movable.get_y_boundary() + 1;
	int speed_of_movable = movable.get_speed();
	int end_top_of_movable = start_top_of_movable - 1 - speed_of_movable;

	for (int j = start_top_of_movable - 1; j >= end_top_of_movable  && j >= 0; j--)
			{
				for (int i = start_LHS_of_movable; i <= start_RHS_of_movable; i++)
				{
					if( _screen_map[i][j] == SAND || _screen_map[i][j] == HOLE)
					{					
						_screen_map[i][j] = DIGDUG;
						_screen_map[i][j + height_of_movable +1] = HOLE;
					}

					//else if( _screen_map[i][j] == HOLE)
					//{					
					//	_screen_map[i][j] = DIGDUG;
					//	_screen_map[i][j + height_of_movable +1] = HOLE;
					//}

					else if(_screen_map[i][j] == MONSTER)
					{
						movable.set_status(DEAD);
						final_move_status = DIED_TRYING_TO_MOVE;
					}
}
	
	
void Game::move_digdug_down( Movable &movable, Move_status &final_move_status)
{
	// return the x-location of the center of the object
	int start_movable_center_x = movable.get_x_location(); 
// return the y-location of the center of the object
	int start_movable_center_y = movable.get_y_location();
// get the coordinates and dimensions of the movable
	int start_top_of_movable = start_movable_center_y - movable.get_y_boundary();
	int start_bottom_of_movable = start_movable_center_y + movable.get_y_boundary();
	int start_LHS_of_movable = start_movable_center_x - movable.get_x_boundary();
	int start_RHS_of_movable = start_movable_center_x + movable.get_x_boundary();
	int height_of_movable = 2*movable.get_y_boundary() + 1;
	int speed_of_movable = movable.get_speed();

	int end_top_of_movable = start_top_of_movable + 1 + speed_of_movable;

	for (int j = start_bottom_of_movable + 1; j <= end_top_of_movable  && j <= _game_grid_elements; j++)
			{
				for (int i = start_LHS_of_movable; i <= start_RHS_of_movable; i++)
				{
					if( _screen_map[i][j] == SAND || _screen_map[i][j] == HOLE)
					{					
						_screen_map[i][j] = DIGDUG;
						_screen_map[i][j - height_of_movable - 1] = HOLE;
						movable.set_location(start_movable_center_x, j)
					}

					//else if( _screen_map[i][j] == HOLE)
					//{					
					//	_screen_map[i][j] = DIGDUG;
					//	_screen_map[i][j + height_of_movable +1] = HOLE;
					//}

					else if(_screen_map[i][j] == MONSTER)
					{
						movable.set_status(DEAD);
						final_move_status = DIED_TRYING_TO_MOVE;
					}
}
	
void Game::move_digdug_left( Movable &movable, Move_status &final_move_status)
{
	// return the x-location of the center of the object
	int start_movable_center_x = movable.get_x_location(); 
// return the y-location of the center of the object
	int start_movable_center_y = movable.get_y_location();
// get the coordinates and dimensions of the movable
	int start_top_of_movable = start_movable_center_y - movable.get_y_boundary();
	int start_bottom_of_movable = start_movable_center_y + movable.get_y_boundary();
	int start_LHS_of_movable = start_movable_center_x - movable.get_x_boundary();
	int start_RHS_of_movable = start_movable_center_x + movable.get_x_boundary();
	int length_of_movable = 2*movable.get_x_boundary() + 1;
	int speed_of_movable = movable.get_speed();

	int end_LHS_of_movable = start_LHS_of_movable - 1 - speed_of_movable;

	for (int i = start_LHS_of_movable - 1; i >= end_LHS_of_movable  && i >= 0; i--)
			{
				for (int j = start_top_of_movable; j <= start_bottom_of_movable; j++)
				{
					if( _screen_map[i][j] == SAND || _screen_map[i][j] == HOLE)
					{					
						_screen_map[i][j] = DIGDUG;
						_screen_map[i + length_of_movable + 1][j] = HOLE;
						movable.set_location(i, start_movable_center_y)
					}

					//else if( _screen_map[i][j] == HOLE)
					//{					
					//	_screen_map[i][j] = DIGDUG;
					//	_screen_map[i][j + height_of_movable +1] = HOLE;
					//}

					else if(_screen_map[i][j] == MONSTER)
					{
						movable.set_status(DEAD);
						final_move_status = DIED_TRYING_TO_MOVE;
					}
}
		
void Game::move_digdug_right( Movable &movable, Move_status &final_move_status)
{
	// return the x-location of the center of the object
	int start_movable_center_x = movable.get_x_location(); 
// return the y-location of the center of the object
	int start_movable_center_y = movable.get_y_location();
// get the coordinates and dimensions of the movable
	int start_top_of_movable = start_movable_center_y - movable.get_y_boundary();
	int start_bottom_of_movable = start_movable_center_y + movable.get_y_boundary();
	int start_LHS_of_movable = start_movable_center_x - movable.get_x_boundary();
	int start_RHS_of_movable = start_movable_center_x + movable.get_x_boundary();
	int length_of_movable = 2*movable.get_x_boundary() + 1;
	int speed_of_movable = movable.get_speed();

	int end_LHS_of_movable = start_RHS_of_movable + 1 + speed_of_movable;

	for (int i = start_RHS_of_movable + 1; i <= end_LHS_of_movable  && i <= _game_grid_element; i++)
			{
				for (int j = start_top_of_movable; j <= start_bottom_of_movable; j++)
				{
					if( _screen_map[i][j] == SAND || _screen_map[i][j] == HOLE)
					{					
						_screen_map[i][j] = DIGDUG;
						_screen_map[i - length_of_movable - 1][j] = HOLE;
						movable.set_location(i, start_movable_center_y)
					}

					//else if( _screen_map[i][j] == HOLE)
					//{					
					//	_screen_map[i][j] = DIGDUG;
					//	_screen_map[i][j + height_of_movable +1] = HOLE;
					//}

					else if(_screen_map[i][j] == MONSTER)
					{
						movable.set_status(DEAD);
						final_move_status = DIED_TRYING_TO_MOVE;
					}
}	
	
	
	
Move_status Game::move_Digdug(Digdug &movable, const Direction &direction)
{
	Move_status final_move_status;

	movable.set_direction(direction);

/*	int length_of_movable = 2*movable.get_x_boundary() + 1;	*/	

	int movable_type = movable.get_object_type();


	if (movable_type == DIGDUG)
	{
		switch(direction)
		{
		case UP:
			move_digdug_up(movable, final_move_status);
			break;
		case DOWN:
			move_digdug_down(movable, final_move_status);
			break;
		case LEFT:
			move_dig_dug_left(movable, final_move_status);
			break;
		case RIGHT:
			move_dig_dug_right(movable, final_move_status);
			break;

	}
			
	if(final_move_status == DIED_TRYING_TO_MOVE)
	{
		return final_move_status;
	}

	int final_movable_center_x = movable.get_x_location();
	int final_movable_center_y = movable.get_y_location();
				
	//movable.set_location(final_movable_center_x, final_movable_center_y);

	if(final_movable_center_x == start_movable_center_x  && final_movable_center_y == start_movable_center_y)
	{
		final_move_status = CANNOT_MOVE;
	}
	else
	{
		final_move_status = CAN_MOVE;
	}
				
	return final_move_status;
}
